package com.lemei.cn.myokhttpbeen.uimanger;

import android.content.Context;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.lemei.cn.myokhttpbeen.R;
import com.lemei.cn.myokhttpbeen.utils.Utils;

import java.util.List;

import static com.lemei.cn.myokhttpbeen.uimanger.Lapager.LOADSTATE.LOADING;

/**
 * Created by 赵航 on 2017/6/6.
 */

public abstract class Lapager extends FrameLayout {

    private View mLaodView;
    private View mErrorView;
    private View mSucessView;

    //第一个是new
    public Lapager(@NonNull Context context) {
        this(context, null);
    }

    //第二个是xml
    public Lapager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init();
    }


    //第三个是style
    public Lapager(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        this(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public Lapager(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        this(context, attrs);
    }

    //将三个布局加载到布局中
    private void init() {
        //加载中的布局
        //加载失败的布局
        //加载成功的布局


        //成功的布局，需要外部传过来
        // 成功布局，谁用谁传

        //加载中
        if (mLaodView==null){

            mLaodView = View.inflate(getContext(), R.layout.page_loading, null);
        }
        addView(mLaodView);
        //加载失败
        if (mErrorView==null){

            mErrorView = View.inflate(getContext(), R.layout.page_error, null);
        }
        addView(mErrorView);

        //抽象
        if (mSucessView==null){

            mSucessView = getSuccesView();
        }
        addView(mSucessView);

        showPager();

        //根据数据自动切换Ui
        changeView();



    }

    /**
     * 填R.layout.成功的xml布局
     *
     * @return
     */
    public abstract View getSuccesView();


    //用枚举定义当前三种状态
    public enum LOADSTATE {

        LOADING, FAIL, SUCCESS

    }

    //先定义初始化状态
    private LOADSTATE curretStata = LOADING;

    //跟距当前状态去显示页面
    public void showPager() {

        //先隐藏所有的界面
        mLaodView.setVisibility(GONE);
        mErrorView.setVisibility(GONE);
        mSucessView.setVisibility(GONE);
        switch (curretStata) {
            case LOADING:
                mLaodView.setVisibility(VISIBLE);
                break;
            case FAIL:
                mErrorView.setVisibility(VISIBLE);
                break;
            case SUCCESS:
                mSucessView.setVisibility(VISIBLE);
                break;
        }
    }

    //根据数据自动切换ui
    public void changeView() {
        new Thread(new Runnable() {
            @Override
            public void run() {


                SystemClock.sleep(2000);
                //根据数据接收 
                Object objct = getNetData();
                //跟距网络返回的数据进行切换
                curretStata = checkData(objct);

                //选择完还需要切换到主界面


                Utils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {

                        //在主线程中切换ui
                        showPager();
                    }
                });


            }
        }).start();
    }

    private LOADSTATE checkData(Object objct) {
        if (objct==null){
            return LOADSTATE.FAIL;
        }else {
            //如果集合的数据为零
            if (objct instanceof List){
                List list = (List) objct;

                if (list.size()>0){
                    return LOADSTATE.SUCCESS;

                }else {
                    return LOADSTATE.FAIL;
                }

            }else {

                return  LOADSTATE.SUCCESS;

            }

        }


    }


    //不确定对象是been还是集合
    public abstract Object getNetData();
}
